package ams.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ams.dao.IBaseDao;
import ams.model.dto.ExecResult;
import ams.model.entity.Rights;
import ams.model.entity.Role;
import ams.model.entity.RoleRights;
import ams.model.entity.UserRole;

@Service("roleService")
public class RoleService implements IRoleService {

	@Autowired
	private IBaseDao<Role> roleDao;
	
	@Autowired
	private IBaseDao<Rights> rightsDao;
	
	@Autowired
	private IBaseDao<RoleRights> roleRightsDao;
	
	@Autowired
	private IBaseDao<UserRole> userRoleDao;

	@Override
	public List<Role> rolePage(Integer page, Integer rows) {
		String hql = "from Role";
		List<Role> roleListPage = roleDao.Find(hql, page, rows);
		return roleListPage;
	}

	@Override
	public Long totalCount() {
		Long count = roleDao.count("select count(*) from Role");
		return count;
	}

	@Override
	public ExecResult add(Role role) {

		ExecResult execResult = new ExecResult();
		List<String> msg = new ArrayList<>();

		String nameIsExistHql = "select count(*) from Role r where r.name = ?";
		String codeIsExistHql = "select count(*) from Role r where r.code = ?";

		boolean nameIsExist = roleDao.count(nameIsExistHql, new Object[] { role.getName() }) != 0 ? true : false;
		boolean codeIsExist = roleDao.count(codeIsExistHql, new Object[] { role.getCode() }) != 0 ? true : false;

		if (nameIsExist) {
			msg.add("角色名称已存在！");
		}
		if (codeIsExist) {
			msg.add("角色编码已存在！");
		}

		if (nameIsExist || codeIsExist) {
			execResult.setSuccess(false);
		} else {
			roleDao.save(role);
			msg.add("角色添加成功！");
			execResult.setSuccess(true);
		}
		execResult.setMessage(msg);
		return execResult;
	}

	@Override
	public ExecResult update(Role role) {

		ExecResult execResult = new ExecResult();
		List<String> msg = new ArrayList<>();

		String nameIsExistHql = "select count(*) from Role r where r.name = ? and r.id != ?";
		String codeIsExistHql = "select count(*) from Role r where r.code = ? and r.id != ?";

		boolean nameIsExist = roleDao.count(nameIsExistHql, new Object[] { role.getName(), role.getId() }) != 0 ? true
				: false;
		boolean codeIsExist = roleDao.count(codeIsExistHql, new Object[] { role.getCode(), role.getId() }) != 0 ? true
				: false;

		if (nameIsExist) {
			msg.add("角色名称已存在！");
		}
		if (codeIsExist) {
			msg.add("角色编码已存在！");
		}

		if (nameIsExist || codeIsExist) {
			execResult.setSuccess(false);
		} else {
			roleDao.update(role);
			execResult.setSuccess(true);
			msg.add("角色修改成功！");
		}
		execResult.setMessage(msg);
		return execResult;
	}

	@Override
	public ExecResult delete(List<Integer> idList) {
		ExecResult execResult = new ExecResult();
		List<String> message = new ArrayList<>();
		//是否有人使用该角色
		String userRoleHql = "select count(*) from UserRole ur where ur.role.id in (:idList)";
		Map<String,Object> paramMap = new HashMap<>();
		paramMap.put("idList", idList);
		Long userRoleCount = userRoleDao.count(userRoleHql, paramMap);
		if(userRoleCount != 0){
			execResult.setSuccess(false);
			message.add("该角色正在使用中！");
		}else{
			String roleRightsHql = "delete from RoleRights rr where rr.role.id in (:idList)";
			//删除角色关联的权限
			roleRightsDao.executeHql(roleRightsHql , paramMap);
			String roleHql = "delete from Role r where r.id in (:idList)";
			//删除角色
			roleDao.executeHql(roleHql, paramMap);
			execResult.setSuccess(true);
		}
		execResult.setMessage(message);
		return execResult;
	}

	@Override
	public Role get(Integer id) {
		String hql = "from Role r where r.id = ?";
		Role role = roleDao.get(hql, new Object[] { id });
		return role;
	}

	@Override
	public ExecResult updateRoleRights(Integer roleId, List<Integer> rightsIdList) {
		ExecResult execResult = new ExecResult();
		String delRoleRightsHql = "delete from RoleRights rr where rr.role.id = ?";
		StringBuilder addRoleRightsSql = new StringBuilder();
		addRoleRightsSql.append("insert into t_role_rights (role_id,rights_id) values");
		for(Integer rigthsId : rightsIdList){
			addRoleRightsSql.append("(");
			addRoleRightsSql.append(roleId);
			addRoleRightsSql.append(",");
			addRoleRightsSql.append(rigthsId);
			addRoleRightsSql.append("),");
		}
		//先删除该角色关联的权限
		roleRightsDao.executeHql(delRoleRightsHql , new Object[]{roleId});
		//再添加新的角色与权限关联
		if(rightsIdList.size() != 0){
			roleRightsDao.executeBySQL(addRoleRightsSql.substring(0, addRoleRightsSql.length()-1));
		}
		execResult.setSuccess(true);
		return execResult;
	}

	@Override
	public List<Role> roleCatalog() {
		List<Role> roleList = new ArrayList<>();
		roleList = roleDao.Find("select new Role(id , name) from Role");
		return roleList;
	}

	@Override
	public List<Rights> getRoleRights(Role role) {
		List<Rights> rightsList = rightsDao.Find("select new Rights(ri.id,ri.pid,ri.name) from Role r inner join r.roleRights rr inner join rr.rights ri where r.id = ?", new Object[]{role.getId()});
		return rightsList;
	}

}
